<?php
require_once ROOT_PATH.'boot/req_inc.php';
abstract class Pluginaction implements Iplugin{
	public $context ;
	function __construct(){
		$this->context = $GLOBALS['context'];
	}
}
class PluginManager extends Pluginaction implements IRequestTool{
	static function register($prop){
		return new PluginManager();
	}
	private function get_web_page($tpl_file, $other_web_page = '') {
		$WEB_PAGE_TPL = 'web_page.tpl.php';
		if($other_web_page) $WEB_PAGE_TPL = str_replace('web_page', 'web_page_'.$other_web_page, $WEB_PAGE_TPL);
		$file = ROOT_PATH.$this->context->app_name.'/plugins'.$tpl_file.'views/'.$WEB_PAGE_TPL;
		if(file_exists($file)){
			return $file;
		} else {
			return false;
		}
	}
	/**
	 * 执行插件
	 * @param $objPlugin 插件类名（插件文件名和类名须一致）
	 * @param $arrParam 参数数组
	 * @param $type true: 插件执行完直接退出， false，插件执行完 继续执行
	 * @param $methodPlungin调用的插件方法名
	 */
   	function activePlugin($objPlugin = null, $arrParam = array(), $type = true,  $methodPlungin = 'plugin_init'){
        try{
        	$app = $this->context->app; //获得当前系统的值
       	 	$classname = load_plugin_model($objPlugin);
       	 	$refclass = new ReflectionClass($classname);
            //区分是不是html  还是逻辑关系
            $ec = $refclass->getMethod($methodPlungin);  //获取fuc 类中的插件方法
       	 	if($ec) {
       	 		$fuc = $refclass->newInstance();  //实例化插件类
       	 		$res = $ec->invoke($fuc, $arrParam);      //执行插件 方法
       	 		if(isset($app) && isset($app['page']) && isset($app['fmt']) &&  $app['fmt'] =='html') {//处理ajax请求
 					$this->context->app['fmt'] = 'pluginhtml';
 					$this->context->app['plugin_path'] = $objPlugin;
       	 		}
       	 		return $res;
       	 		/*
       	 		if(isset($app) && isset($app['page']) && isset($app['fmt']) && $app['page'] == 'null' && $app['fmt'] =='json') {//处理ajax请求
       	 			print_r(json_encode($res));
       	 			exit;
       	 		} else {
       	 			//处理页面加载
       	 			$webpage_charset = $this->context->get_app_conf('charset') ? $this->context->get_app_conf('charset'): 'utf-8';
       	 			$tpl = isset($res['tpl']) ? $res['tpl'] .'.tpl.php': 'index.tpl.php';
       	 			$main_child_tpl = ROOT_PATH.$this->context->app_name.'/plugins'.$objPlugin.'views/'.$tpl;
       	 			if(isset($app['page']) && $app['page'] = 'NULL') {
       	 				include $main_child_tpl;
       	 				if($type) {
       	 					exit;
       	 				}
       	 			}else {
       	 				ob_start();
       	 				$_web_page_file = $this->get_web_page ($objPlugin);
       	 				if($_web_page_file)  {
       	 					$_FASTAPP_web_page_file_3D8jKw5L2q  = $_web_page_file;
       	 				}
	       	 			if(file_exists($_FASTAPP_web_page_file_3D8jKw5L2q)) {
	       	 				include $_FASTAPP_web_page_file_3D8jKw5L2q;
	       	 			} else {
	       	 				$_FASTAPP_web_page_file_3D8jKw5L2q = $main_child_tpl;
		       	 			echo "<html><head>\n<meta http-equiv='content-Type' content='text/html; charset=".
								$GLOBALS['context']->get_app_conf('charset')."' />\n";
							echo  "</head><body>\n";
							include $_FASTAPP_web_page_file_3D8jKw5L2q;
							echo "\n</body></html>";
	       	 			}
       	 			}
       	 			$html = ob_get_contents();
					ob_end_clean();
					echo $html;
					if($type) {
						exit;
					}
       	 		}
       	 		*/
       	 	}else {
       	 		$this->context->log_error($objPlugin.' done plugin failure ,do not find the method ： '.$methodPlungin);
       	 		return null;
       	 	}
        }catch(Exception $ex){
         	$this->context->log_error($objPlugin.' done plugin failure ,message:'.$ex->getMessage());
        }
   	}
}
